<?
/*
Cogumelo v0.1 - Map-Experience
Copyright (C) 2008 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@map-experience.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/
require_once("Classes/Model/Facade/Facade.inc");
require_once("Classes/Model/VO/UseradminVO.inc");


//
//	Useradmin Facade (extends the Facade class)
//

class UseradminFacade extends Facade
{
	private $dao;
	
	//
	// Useradmin Facade 
	//
	function __construct()
	{		
		parent::__construct();
		$this->dao = $this->datasrc->getuseradminDAO();
	}
	
		
	//
	// Create New Useradmin from useradminVO. Returns new useradminID
	//
	function Create($useradmin)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Useradmin Creation", 4);
		$this->OpenConnection(); 		
		$id = $this->dao->Create($this->connection, $useradmin);		
		$this->CloseConnection();		
		if($id) $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin Created with ID=".$id, 4);		
		return $id;
	}

	//
	// Create New Useradmin from useradminVO. Returns new useradminID
	//
	function Update($useradmin)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Useradmin Update", 4);
		$this->OpenConnection(); 		
		$id = $this->dao->Update($this->connection, $useradmin);		
		$this->CloseConnection();		
		if($id) $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin Updated with ID=".$id, 4);		
		return $id;
	}
	
	

	//
	// Destroy: total delete of an useradmin 
	//
	function Destroy($id)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Useradmin Destruction with ID=".$id, 4);
		$this->OpenConnection(); 	
		if($oldvo=$this->dao->Find($this->connection, $id))
		 {
		  $oldvo = $this->dao->Destroy($this->connection, $id);		  
		  $this->CloseConnection();
		  $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin Destruction with ID=".$id, 4);	
		  return $oldvo;
		 }
		 else
		 {
		    $this->logcontrol->addLogLine("TRANSACTION FAILED: Useradmin NOT FOUND with ID=".$id, 4);
		 	$this->CloseConnection();
		 	return false;
		 }
	}
	
	//
	// Update:  updatepass 
	//
	function Updatepass($newupdateadmin)
	{
		$id = $newupdateadmin->getId();
	    $this->logcontrol->addLogLine("TRANSACTION START: Useradmin Password change with ID=".$id, 4);
	    $this->OpenConnection(); 
		$oldvo = $this->dao->UpdatePasswd($this->connection, $newupdateadmin);	
		$this->CloseConnection();		
		if($oldvo) $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin Password changed with ID=".$id, 4);
		return $oldvo;
		
	}
	
	//
	// Find: return an useradmin by id
	//
	function Find($id)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Find Useradmin with ID=".$id, 4);
		$this->OpenConnection();
		$data = $this->dao->Find($this->connection, $id);
		$this->CloseConnection();
		if($data) $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Found Useradmin with ID=".$id, 4);
		else $this->logcontrol->addLogLine("TRANSACTION COMPLETED: NOT Found Useradmin with ID=".$id, 4);
		return $data;
	}
	
	//
	// FindByLogin: return an useradmin by e-mail
	//
	function FindByLogin($login)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Find Useradmin with login=".$login, 4);
		$this->OpenConnection();
		$data = $this->dao->FindByLogin($this->connection, $login);
		$this->CloseConnection();
		if($data) $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Found Useradmin with login=".$id, 4);
		else $this->logcontrol->addLogLine("TRANSACTION COMPLETED: NOT Found Useradmin with login=".$id, 4);
		return $data;
	}
	
		
	//
	// ListUseradmins, return useradminVO array.
	//
	function ListUseradmins($filter, $RANGE, $ORDER)
	{
	    $this->logcontrol->addLogline("TRANSACTION START: List All user admins", 4);
		$this->OpenConnection();
		$data = $this->dao->ListAll($this->connection, $filter, $RANGE,  $ORDER);
		$this->CloseConnection;
		if($data) $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin list generated", 4);
		else $this->logcontrol->addLogLine("TRANSACTION COMPLETED: NOT found useradmins", 4);
		return $data;
	}
	//
	// ListUseradminsCount (Rows number)
	function ListUseradminsCount($filter)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Count All user admins", 4);
		$this->OpenConnection();
		$data = $this->dao->ListAllCount($this->connection, $filter);
		$this->CloseConnection;
		$this->logcontrol->addLogLine("TRANSACTION COMPLETED: Counted ".$data." Useradmins", 4);
		return $data;
	}
	

	/*
	 * Authenticate an useradmin for login. Return user information.
	 */
	function AuthenticateUseradmin($userAdmin)
	{
	    $this->logcontrol->addLogLine("TRANSACTION START: Useradmin authentication with login=".$userAdmin->getLogin(), 4);	    
		$this->OpenConnection();			
		if($user=$this->dao->AuthenticateUseradmin($this->connection, $userAdmin->getLogin(), $userAdmin->getPasswd()))
		{
		$rightnow=date("YmdHis", time());				 
		$user->SetTimeLastLogin($rightnow);
		$this->dao->UpdateTime($this->connection, $user);
		$this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin authenticated with login=".$userAdmin->getLogin(), 4);
		return($user);
		}
		else { $this->logcontrol->addLogLine("TRANSACTION COMPLETED: Useradmin".$userAdmin->getLogin()." NOT authenticated", 4); return(false); }
		$this->CloseConnection;
		
	}
	
	
}
?>